import axios from "axios";
import { Toast } from 'react-vant';
const instance = axios.create();
instance.defaults.timeout = 2500;
// Add a request interceptor
instance.interceptors.request.use(function (config) {
    let token = window.localStorage.getItem("token") || ""
    config.headers['Authorization'] = 'Bearer '+ token;
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
instance.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
      switch (error.response.status) {
         case "401" :
            Toast.fail("token无效")
            break;
        case "404" :
            Toast.fail("接口找不到")
            break;
        case "500" :
            Toast.fail("服务端异常")
            break;
        case "502" :
            Toast.fail("服务端错误")
            break;
        default :
            Toast.fail("未知错误")
      }
    // Do something with response error
    return Promise.reject(error);
});
export default instance
